package com.zl.zlproject.base.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.gyf.barlibrary.ImmersionBar;
import com.zl.zlproject.base.application.MyApplication;

import butterknife.ButterKnife;

/**
 * Created by 郑磊 on 2018/5/27.
 */

public abstract class BaseFragment  extends Fragment {
    protected View mRoot;
    protected ImmersionBar mStatusBar;
    protected String mScreenName;
    protected Context mContext;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (mRoot == null) {
            int layId = getContentLayoutId();
            // 初始化当前的跟布局
            mRoot = inflater.inflate(layId, container, false);

        }
        return mRoot;
    }


    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mScreenName = getClass().getSimpleName();
        mContext =getContext();
        initBefore();
        ButterKnife.bind(this,mRoot);
        mStatusBar = ImmersionBar.with(this);
        mStatusBar.init();
        initView();
        initData();
        initListener();
    }

    protected  void initBefore(){}

    protected void initView() {
    }

    /**
     * 初始化toolbar
     *
     * @param toolbar Toolbar
     */
    public void initToolbar(Toolbar toolbar, View backIcon, boolean showBackIcon) {
        toolbar.setBackgroundResource(MyApplication.appColor);
        initStatusBar(toolbar);
        toolbar.setTitle("");
        if (backIcon != null) {
            backIcon.setVisibility(showBackIcon ? View.VISIBLE :View.GONE);
            backIcon.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    getActivity().finish();
                }
            });
        }
    }

    protected  void initStatusBar(Toolbar toolbar){
        mStatusBar.titleBar(toolbar).init();
    }

    protected void initData() {

    }

    protected void initListener() {

    }


    protected abstract int getContentLayoutId();

    @Override
    public void onDestroy() {
        super.onDestroy();
        ImmersionBar.with(this).destroy();
    }
}
